#define  _CRT_SECURE_NO_WARNINGS 1

#include "string.h"

namespace zzh
{
	string::string(const char* s)
	{
		_size = strlen(s);
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, s);
	}

	const char* string::c_str() const
	{
		return _str;
	}

	size_t string::size() const
	{
		return _size;
	}

	size_t string::capacity() const
	{
		return _capacity;
	}

	void string::reserve(int n)
	{
		if (n > _capacity)
		{
			char* temp = new char[n + 1];
			strcpy(temp, _str);
			delete[] _str;
			_str = temp;
			_capacity = n;
		}
	}

	void string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2 + 1);
		}
		_str[_size++] = ch;
		_str[_size] = '\0';
	}

	void string::append(const char* s)
	{
		size_t len = strlen(s);
		if (_size + len > _capacity)
		{
			reserve(_size + len < _capacity * 2 ? _capacity * 2 : _size + len);
		}
		strcpy(_str + _size, s);
		_size += len;
	}

	string& string::operator+=(char ch)
	{
		push_back(ch);

		return *this;
	}

	string& string::operator+=(const char* s)
	{
		append(s);

		return *this;
	}

	char& string::operator[](size_t pos)
	{
		assert(pos < _size);

		return _str[pos];
	}

	const char& string::operator[](size_t pos) const
	{
		assert(pos < _size);

		return _str[pos];
	}

	string::iterator string::begin()
	{
		return _str;
	}

	string::iterator string::end()
	{
		return _str + _size;
	}


}